package dto

import (
	"encoding/json"
	"time"

	"wxpay/internal/model"
	"wxpay/pkg/util"

	"gorm.io/datatypes"
)

type BusinessLicenseInfoDTO struct {
	// 证件扫描件 - MediaID
	BusinessLicenseCopy string
	// 证件扫描件的oss名称
	CopyOssName string
	// 证件注册号
	BusinessLicenseNumber string
	// 商户名称
	MerchantName string
	// 经营者/法定代表人姓名
	LegalPerson string
	// 注册地址
	CompanyAddress string
	// 营业期限
	BusinessTime string
}

type OrganizationCertInfoDTO struct {
	// 组织机构代码证照片
	OrganizationCopy string
	// 证件扫描件的oss名称
	CopyOssName string
	// 组织机构代码
	OrganizationNumber string
	// 组织机构代码有效期限
	OrganizationTime string
}

type IDCardInfoDTO struct {
	// 身份证人像面照片
	IDCardCopy string
	// 证件扫描件的oss名称
	CardCopyOssName string
	// 身份证国徽面照片
	IDCardNational string
	// 证件扫描件的oss名称
	NationalCopyOssName string
	// 身份证姓名
	IDCardName string
	// 身份证号码
	IDCardNumber string
	// 身份证有效期限
	IDCardValidTime string
}

type IDDocInfoDTO struct {
	// 证件姓名
	IDDocName string
	// 证件号码
	IDDocNumber string
	// 证件照片
	IDDocCopy string
	// 证件扫描件的oss名称
	CopyOssName string
	// 证件结束日期
	DocPeriodEnd string
}

type AccountInfoDTO struct {
	// 账户类型
	BankAccountType string
	// 开户银行
	AccountBank string
	// 开户名称
	AccountName string
	// 开户银行省市编码
	BankAddressCode string
	// 开户银行联行号
	BankBranchID string
	// 开户银行全称 （含支行）
	BankName string
	// 银行帐号
	AccountNumber string
}

type ContactInfoDTO struct {
	// 超级管理员类型
	ContactType string
	// 超级管理员姓名
	ContactName string
	// 超级管理员身份证件号码
	ContactIDCardNumber string
	// 超级管理员手机
	MobilePhone string
	// 超级管理员邮箱
	ContactEmail string
}

type SalesSceneInfoDTO struct {
	// 店铺名称
	StoreName string
	// 店铺链接
	StoreUrl string
	// 店铺二维码
	StoreQrCode string
	// 小程序AppID
	MiniProgramSubAppID string
}

type ApplymentInfoDTO struct {
	// 主体类型
	OrganizationType string
	// 营业执照/登记证书信息
	BusinessLicenseInfo *BusinessLicenseInfoDTO
	// 组织机构代码证信息
	OrganizationCertInfo *OrganizationCertInfoDTO
	// 经营者/法人证件类型
	IDDocType string
	// 经营者/法人身份证信息
	IDCardInfo *IDCardInfoDTO
	// 经营者/法人其他类型证件信息
	IDDocInfo *IDDocInfoDTO
	// 是否填写结算银行账户
	NeedAccountInfo bool
	// 结算银行账户
	AccountInfo *AccountInfoDTO
	// 超级管理员信息
	ContactInfo ContactInfoDTO
	// 店铺信息
	SalesSceneInfo SalesSceneInfoDTO
	// 商户简称
	MerchantShortname string
	// 特殊资质
	Qualifications []string
	// 补充材料
	BusinessAdditionPics []string
	// 补充说明
	BusinessAdditionDesc string
}

type AuditDTO struct {
	ApplyState         string
	ApplyStateDesc     string
	SignURL            string
	AccountValidation  *AccountValidationDTO
	SubMchID           string
	AuditDetail        *AuditDetailDTO
	LegalValidationURL string
}

// 汇款账户验证信息
type AccountValidationDTO struct {
	// 付款户名
	AccountName string
	// 付款卡号
	AccountNo string
	// 汇款金额 （以分为单位）
	PayAmount string
	// 收款卡号
	DestinationAccountNumber string
	// 收款户名
	DestinationAccountName string
	// 开户银行
	DestinationAccountBank string
	// 省市信息
	City string
	// 备注信息
	Remark string
	// 汇款截止时间
	Deadline string
}

// 驳回原因详情
type AuditDetailDTO []struct {
	// 参数名称
	ParamName string
	// 驳回原因
	RejectReason string
}

type ApplymentDTO struct {
	ID                 int64
	ApplyNo            string
	BizApplyNo         string
	BrandID            string
	MchID              string
	ApplymentType      string
	ApplymentInfo      *ApplymentInfoDTO
	WxApplymentID      string
	ApplyState         string
	ApplyStateDesc     string
	SignURL            string
	AccountValidation  *AccountValidationDTO
	SubMchID           string
	AuditDetail        AuditDetailDTO
	LegalValidationURL string
	CreateTime         time.Time
	UpdateTime         time.Time
}

func ConvertApplymentInfo(info *model.ApplymentInfo) *ApplymentInfoDTO {
	res := ApplymentInfoDTO{
		OrganizationType:     info.OrganizationType,
		BusinessLicenseInfo:  convertBusinessLicenseInfo(info.BusinessLicenseInfo),
		OrganizationCertInfo: convertOrganizationCertInfo(info.OrganizationCertInfo),
		IDDocType:            info.IDDocType,
		IDCardInfo:           convertIDCardInfo(info.IDCardInfo),
		IDDocInfo:            convertIDDoc(info.IDDocInfo),
		NeedAccountInfo:      info.NeedAccountInfo,
		AccountInfo:          convertAccountInfo(info.AccountInfo),
		ContactInfo: ContactInfoDTO{
			ContactType:         info.ContactInfo.ContactType,
			ContactName:         info.ContactInfo.ContactName,
			ContactIDCardNumber: info.ContactInfo.ContactIDCardNumber,
			MobilePhone:         info.ContactInfo.MobilePhone,
			ContactEmail:        info.ContactInfo.ContactEmail,
		},
		SalesSceneInfo: SalesSceneInfoDTO{
			StoreName:           info.SalesSceneInfo.StoreName,
			StoreUrl:            info.SalesSceneInfo.StoreURL,
			StoreQrCode:         info.SalesSceneInfo.StoreQrCode,
			MiniProgramSubAppID: info.SalesSceneInfo.MiniProgramSubAppID,
		},
		MerchantShortname:    info.MerchantShortname,
		Qualifications:       info.Qualifications,
		BusinessAdditionPics: info.BusinessAdditionPics,
		BusinessAdditionDesc: info.BusinessAdditionDesc,
	}
	return &res
}

// model to dto
func ConvertApplyment(apply *model.Applyment) *ApplymentDTO {
	info := &model.ApplymentInfo{}
	info.ToStruct(apply.ApplymentInfo)
	acct := &model.AccountValidation{}
	acct.ToStruct(apply.AccountValidation)
	audits := &model.AuditDetails{}
	audits.ToStruct(apply.AuditDetail)
	res := ApplymentDTO{
		ID:                 apply.ID,
		ApplyNo:            apply.ApplyNo,
		BizApplyNo:         apply.BizApplyNo,
		BrandID:            apply.BrandID,
		MchID:              apply.MchID,
		ApplymentType:      apply.ApplymentType,
		ApplymentInfo:      ConvertApplymentInfo(info),
		WxApplymentID:      util.ConvertNullInt64(apply.WxApplymentID),
		ApplyState:         util.ConvertNullString(apply.ApplyState),
		ApplyStateDesc:     util.ConvertNullString(apply.ApplyStateDesc),
		SignURL:            util.ConvertNullString(apply.SignURL),
		AccountValidation:  convertAccountValidation(acct),
		SubMchID:           util.ConvertNullString(apply.SubMchID),
		AuditDetail:        convertAuditDetail(*audits),
		LegalValidationURL: util.ConvertNullString(apply.LegalValidationURL),
		CreateTime:         apply.CreateTime,
		UpdateTime:         apply.UpdateTime,
	}
	return &res
}

func convertAuditDetail(detail model.AuditDetails) (r AuditDetailDTO) {
	if len(detail) == 0 {
		return
	}
	for _, d := range detail {
		r = append(r, struct {
			ParamName    string
			RejectReason string
		}{ParamName: d.ParamName, RejectReason: d.RejectReason})
	}
	return
}

func convertAccountValidation(account *model.AccountValidation) (r *AccountValidationDTO) {
	if account == nil {
		return
	}
	r = &AccountValidationDTO{
		AccountName:              account.AccountName,
		AccountNo:                account.AccountNo,
		PayAmount:                account.PayAmount,
		DestinationAccountNumber: account.DestinationAccountNumber,
		DestinationAccountName:   account.DestinationAccountName,
		DestinationAccountBank:   account.DestinationAccountBank,
		City:                     account.City,
		Remark:                   account.Remark,
		Deadline:                 account.Deadline,
	}
	return
}

func convertBusinessLicenseInfo(blinfo *model.BusinessLicenseInfo) (r *BusinessLicenseInfoDTO) {
	if blinfo == nil {
		return
	}
	r = &BusinessLicenseInfoDTO{
		BusinessLicenseCopy:   blinfo.BusinessLicenseCopy,
		CopyOssName:           blinfo.CopyOssName,
		BusinessLicenseNumber: blinfo.BusinessLicenseNumber,
		MerchantName:          blinfo.MerchantName,
		LegalPerson:           blinfo.LegalPerson,
		CompanyAddress:        blinfo.CompanyAddress,
		BusinessTime:          blinfo.BusinessTime,
	}
	return
}

func convertOrganizationCertInfo(ocinfo *model.OrganizationCertInfo) (r *OrganizationCertInfoDTO) {
	if ocinfo == nil {
		return
	}
	r = &OrganizationCertInfoDTO{
		OrganizationCopy:   ocinfo.OrganizationCopy,
		CopyOssName:        ocinfo.CopyOssName,
		OrganizationNumber: ocinfo.OrganizationNumber,
		OrganizationTime:   ocinfo.OrganizationTime,
	}
	return
}

func convertIDCardInfo(idcard *model.IDCardInfo) (r *IDCardInfoDTO) {
	if idcard == nil {
		return
	}
	r = &IDCardInfoDTO{
		IDCardCopy:          idcard.IDCardCopy,
		CardCopyOssName:     idcard.CardOssName,
		IDCardNational:      idcard.IDCardNational,
		NationalCopyOssName: idcard.NationalOssName,
		IDCardName:          idcard.IDCardName,
		IDCardNumber:        idcard.IDCardNumber,
		IDCardValidTime:     idcard.IDCardValidTime,
	}
	return
}

func convertIDDoc(iddoc *model.IDDocInfo) (r *IDDocInfoDTO) {
	if iddoc == nil {
		return
	}
	r = &IDDocInfoDTO{
		IDDocName:    iddoc.IDDocName,
		IDDocNumber:  iddoc.IDDocNumber,
		IDDocCopy:    iddoc.IDDocCopy,
		CopyOssName:  iddoc.CopyOssName,
		DocPeriodEnd: iddoc.DocPeriodEnd,
	}
	return
}

func convertAccountInfo(account *model.AccountInfo) (r *AccountInfoDTO) {
	if account == nil {
		return
	}
	r = &AccountInfoDTO{
		BankAccountType: account.BankAccountType,
		AccountBank:     account.AccountBank,
		AccountName:     account.AccountName,
		BankAddressCode: account.BankAddressCode,
		BankBranchID:    account.BankBranchID,
		BankName:        account.BankName,
		AccountNumber:   account.AccountNumber,
	}
	return
}

// dto to model
func AdaptApplyment(info *ApplymentDTO) (r *model.Applyment) {
	r = &model.Applyment{
		ID:                 info.ID,
		ApplyNo:            info.ApplyNo,
		BizApplyNo:         info.BizApplyNo,
		BrandID:            info.BrandID,
		MchID:              info.MchID,
		ApplymentType:      info.ApplymentType,
		ApplymentInfo:      AdaptApplymentInfo(info.ApplymentInfo),
		WxApplymentID:      util.AdaptNullInt64(info.WxApplymentID),
		ApplyState:         util.AdaptNullString(info.ApplyState),
		ApplyStateDesc:     util.AdaptNullString(info.ApplyStateDesc),
		SignURL:            util.AdaptNullString(info.SignURL),
		AccountValidation:  AdaptAccountValidation(info.AccountValidation),
		SubMchID:           util.AdaptNullString(info.SubMchID),
		AuditDetail:        AdaptAuditDetail(info.AuditDetail),
		LegalValidationURL: util.AdaptNullString(info.LegalValidationURL),
		CreateTime:         info.CreateTime,
		UpdateTime:         info.UpdateTime,
	}
	return
}

func AdaptBusinessLicenseInfo(info *BusinessLicenseInfoDTO) (r *model.BusinessLicenseInfo) {
	if info == nil {
		return
	}
	r = &model.BusinessLicenseInfo{
		BusinessLicenseCopy:   info.BusinessLicenseCopy,
		CopyOssName:           info.CopyOssName,
		BusinessLicenseNumber: info.BusinessLicenseNumber,
		MerchantName:          info.MerchantName,
		LegalPerson:           info.LegalPerson,
		CompanyAddress:        info.CompanyAddress,
		BusinessTime:          info.BusinessTime,
	}
	return
}

func AdaptOrganizationCertInfo(info *OrganizationCertInfoDTO) (r *model.OrganizationCertInfo) {
	if info == nil {
		return
	}
	r = &model.OrganizationCertInfo{
		OrganizationCopy:   info.OrganizationCopy,
		CopyOssName:        info.CopyOssName,
		OrganizationNumber: info.OrganizationNumber,
		OrganizationTime:   info.OrganizationTime,
	}
	return
}

func AdaptIDCardInfo(info *IDCardInfoDTO) (r *model.IDCardInfo) {
	if info == nil {
		return
	}
	r = &model.IDCardInfo{
		IDCardCopy:      info.IDCardCopy,
		CardOssName:     info.CardCopyOssName,
		IDCardNational:  info.IDCardNational,
		NationalOssName: info.NationalCopyOssName,
		IDCardName:      info.IDCardName,
		IDCardNumber:    info.IDCardNumber,
		IDCardValidTime: info.IDCardValidTime,
	}
	return
}

func AdaptIDDocInfo(info *IDDocInfoDTO) (r *model.IDDocInfo) {
	if info == nil {
		return
	}
	r = &model.IDDocInfo{
		IDDocName:    info.IDDocName,
		IDDocNumber:  info.IDDocNumber,
		IDDocCopy:    info.IDDocCopy,
		CopyOssName:  info.CopyOssName,
		DocPeriodEnd: info.DocPeriodEnd,
	}
	return
}

func AdaptAccountInfo(info *AccountInfoDTO) (r *model.AccountInfo) {
	if info == nil {
		return
	}
	r = &model.AccountInfo{
		BankAccountType: info.BankAccountType,
		AccountBank:     info.AccountBank,
		AccountName:     info.AccountName,
		BankAddressCode: info.BankAddressCode,
		BankBranchID:    info.BankBranchID,
		BankName:        info.BankName,
		AccountNumber:   info.AccountNumber,
	}
	return
}

func AdaptContactInfo(info ContactInfoDTO) (r model.ContactInfo) {
	r = model.ContactInfo{
		ContactType:         info.ContactType,
		ContactName:         info.ContactName,
		ContactIDCardNumber: info.ContactIDCardNumber,
		MobilePhone:         info.MobilePhone,
		ContactEmail:        info.ContactEmail,
	}
	return
}

func AdaptSalesSceneInfo(info SalesSceneInfoDTO) (r model.SalesSceneInfo) {
	r = model.SalesSceneInfo{
		StoreName:           info.StoreName,
		StoreURL:            info.StoreUrl,
		StoreQrCode:         info.StoreQrCode,
		MiniProgramSubAppID: info.MiniProgramSubAppID,
	}
	return
}

func AdaptApplymentInfo(info *ApplymentInfoDTO) (r datatypes.JSON) {
	if info == nil {
		return
	}
	m := &model.ApplymentInfo{
		OrganizationType:     info.OrganizationType,
		BusinessLicenseInfo:  AdaptBusinessLicenseInfo(info.BusinessLicenseInfo),
		OrganizationCertInfo: AdaptOrganizationCertInfo(info.OrganizationCertInfo),
		IDDocType:            info.IDDocType,
		IDCardInfo:           AdaptIDCardInfo(info.IDCardInfo),
		IDDocInfo:            AdaptIDDocInfo(info.IDDocInfo),
		NeedAccountInfo:      info.NeedAccountInfo,
		AccountInfo:          AdaptAccountInfo(info.AccountInfo),
		ContactInfo:          AdaptContactInfo(info.ContactInfo),
		SalesSceneInfo:       AdaptSalesSceneInfo(info.SalesSceneInfo),
		MerchantShortname:    info.MerchantShortname,
		Qualifications:       info.Qualifications,
		BusinessAdditionPics: info.BusinessAdditionPics,
		BusinessAdditionDesc: info.BusinessAdditionDesc,
	}
	r = m.ToJSON()
	return
}

func AdaptAccountValidation(info *AccountValidationDTO) (r datatypes.JSON) {
	if info == nil {
		return
	}
	m := model.AccountValidation{
		AccountName:              info.AccountName,
		AccountNo:                info.AccountNo,
		PayAmount:                info.PayAmount,
		DestinationAccountNumber: info.DestinationAccountNumber,
		DestinationAccountName:   info.DestinationAccountName,
		DestinationAccountBank:   info.DestinationAccountBank,
		City:                     info.City,
		Remark:                   info.Remark,
		Deadline:                 info.Deadline,
	}
	r = m.ToJSON()
	return
}

func AdaptAuditDetail(info AuditDetailDTO) (r datatypes.JSON) {
	if info == nil {
		return
	}
	m := make(model.AuditDetails, len(info))
	for _, d := range info {
		m = append(m, model.AuditDetail{ParamName: d.ParamName, RejectReason: d.RejectReason})
	}
	r = m.ToJSON()
	return
}

type ContactInfoV2DTO struct {
	// 超级管理员姓名
	ContactName string
	// 超级管理员身份证件号码
	ContactIdNumber string
	// 联系手机
	MobilePhone string
	// 联系邮箱
	ContactEmail string
}

// 特约商户进件
type BusinessLicenseInfoV2DTO struct {
	// 营业执照照片
	LicenseCopy    string
	OssLicenseCopy string
	// 注册号/统一社会信用代码
	LicenseNumber string
	// 商户名称
	MerchantName string
	// 个体户经营者/法人姓名
	LegalPerson string
}

// 登记证书 主体为党政、机关及事业单位/其他组织，必填
type CertificateInfoDTO struct {
	// 登记证书照片
	CertCopy    string
	OssCertCopy string
	// 登记证书类型
	CertType string
	// 证书号
	CertNumber string
	// 商户名称
	MerchantName string
	// 注册地址
	CompanyAddress string
	// 法人姓名
	LegalPerson string
	// 有效期限开始日期
	PeriodBegin string
	// 有效期限结束日期
	PeriodEnd string
}

type OrganizationInfoDTO struct {
	// 组织机构代码证照片
	OrganizationCopy    string
	OssOrganizationCopy string
	// 组织机构代码
	OrganizationCode string
	// 组织机构代码证有效期开始日期
	OrgPeriodBegin string
	// 组织机构代码证有效期结束日期
	OrgPeriodEnd string
}

// 身份证信息
type IdCardInfoV2DTO struct {
	// 身份证人像面照片
	IdCardCopy    string
	OssIdCardCopy string
	// 身份证国徽面照片
	IdCardNational    string
	OssIdCardNational string
	// 身份证姓名
	IdCardName string
	// 身份证号码
	IdCardNumber string
	// 身份证有效期开始时间
	CardPeriodBegin string
	// 身份证有效期结束时间
	CardPeriodEnd string
}

// 其他类型证件信息
type IDDocInfoV2DTO struct {
	// 证件照片
	IdDocCopy    string
	OssIdDocCopy string
	// 证件姓名
	IdDocName string
	// 证件号码
	IdDocNumber string
	//  证件有效期开始时间
	DocPeriodBegin string
	// 证件有效期结束时间
	DocPeriodEnd string
}

// 经营者/法人身份证件
type IdentityInfoDTO struct {
	// 证据类型
	IdDocType string
	// 身份证信息
	IdCardInfoDTO *IdCardInfoV2DTO
	// 其他类型证件
	IdDocInfoDTO *IDDocInfoV2DTO
	//是否为最终受益人
	Owner bool
}

// 最终受益人信息
type UboInfoDTO struct {
	// 证件类型
	IdType string
	// 身份证人像面照片
	IdCardCopy    string
	OssIdCardCopy string
	// 身份证国徽面照片
	IdCardNational    string
	OssIdCardNational string
	// 证件照片
	IdDocCopy    string
	OssIdDocCopy string
	// 受益人姓名
	Name string
	// 证件号码
	IdNumber string
	// 证件有效期开始时间
	IdPeriodBegin string
	// 证件有效期结束时间
	IdPeriodEnd string
}

// 主体资料
type SubjectInfoDTO struct {
	// 主体类型
	SubjectType string
	// 营业执照
	BusinessLicenseInfoDTO *BusinessLicenseInfoV2DTO
	// 登记证书
	CertificateInfoDTO *CertificateInfoDTO
	// 组织机构代码证
	OrganizationInfoDTO *OrganizationInfoDTO
	// 单位证明函照片
	CertificateLetterCopy    string
	OssCertificateLetterCopy string
	// 经营者/法人身份证件
	IdentityInfoDTO *IdentityInfoDTO
	// 最终受益人信息
	UboInfoDTO *UboInfoDTO
	// 小微服务证明材料
	MicroBizInfoDTO *MicroBizInfoDTO
}

// 门店场所
type MicroStoreInfoDTO struct {
	// 门店名称
	MicroName string
	// 门店省市编码
	MicroAddressCode string
	// 门店地址
	MicroAddress string
	// 门店门头照片
	StoreEntrancePic    string
	OssStoreEntrancePic string
	// 门店店内环境照片
	MicroIndoorCopy    string
	OssMicroIndoorCopy string
	// 门店经度
	StoreLongitude string
	// 门店纬度
	StoreLatitude string
}

// 流动经营/便民服务
type MicroMobileInfoDTO struct {
	// 经营/服务名称
	MicroMobileName string
	// 经营/服务所在地省市
	MicroMobileCity string
	// 经营/服务所在地（不含省市]
	MicroMobileAddress string
	// 经营服务现场照片
	MicroMobilePics    []string
	OssMicroMobilePics []string
}

// 线上商品/服务交易
type MicroOnlineInfoDTO struct {
	// 线上店铺名称
	MicroOnlineStore string
	// 电商平台名称
	MicroEcName string
	// 店铺二维码
	MicroQrcode    string
	OssMicroQrcode string
	// 店铺链接
	MicroLink string
}

// 小微证明材料
type MicroBizInfoDTO struct {
	// 微信经营类型
	MicroBizType string
	// 门店场所
	MicroStoreInfoDTO *MicroStoreInfoDTO
	// 流动经营/便民服务
	MicroMobileInfoDTO *MicroMobileInfoDTO
	// 线上商品服务交易
	MicroOnlineInfoDTO *MicroOnlineInfoDTO
}

func ConvertSubjectInfo(s *model.SubjectInfo) *SubjectInfoDTO {
	if s == nil {
		return nil
	}
	r := &SubjectInfoDTO{
		SubjectType:              s.SubjectType,
		CertificateLetterCopy:    s.CertificateLetterCopy,
		OssCertificateLetterCopy: s.OssCertificateLetterCopy,
		UboInfoDTO:               nil,
	}
	if s.BusinessLicenseInfo != nil {
		bus := s.BusinessLicenseInfo
		r.BusinessLicenseInfoDTO = &BusinessLicenseInfoV2DTO{
			LicenseCopy:    bus.LicenseCopy,
			OssLicenseCopy: bus.OssLicenseCopy,
			LicenseNumber:  bus.LicenseNumber,
			MerchantName:   bus.MerchantName,
			LegalPerson:    bus.LegalPerson,
		}
	}
	if s.CertificateInfo != nil {
		cert := s.CertificateInfo
		r.CertificateInfoDTO = &CertificateInfoDTO{
			CertCopy:       cert.CertCopy,
			OssCertCopy:    cert.OssCertCopy,
			CertType:       cert.CertType,
			CertNumber:     cert.CertNumber,
			MerchantName:   cert.MerchantName,
			CompanyAddress: cert.CompanyAddress,
			LegalPerson:    cert.LegalPerson,
			PeriodBegin:    cert.PeriodBegin,
			PeriodEnd:      cert.PeriodEnd,
		}
	}
	if s.OrganizationInfo != nil {
		org := s.OrganizationInfo
		r.OrganizationInfoDTO = &OrganizationInfoDTO{
			OrganizationCopy:    org.OrganizationCopy,
			OssOrganizationCopy: org.OssOrganizationCopy,
			OrganizationCode:    org.OrganizationCode,
			OrgPeriodBegin:      org.OrgPeriodBegin,
			OrgPeriodEnd:        org.OrgPeriodEnd,
		}
	}
	if s.IdentityInfo != nil {
		iden := s.IdentityInfo
		r.IdentityInfoDTO = &IdentityInfoDTO{
			IdDocType: iden.IdDocType,
			Owner:     iden.Owner,
		}
		if iden.IdCardInfo != nil {
			r.IdentityInfoDTO.IdCardInfoDTO = &IdCardInfoV2DTO{
				IdCardCopy:        iden.IdCardInfo.IdCardCopy,
				OssIdCardCopy:     iden.IdCardInfo.OssIdCardCopay,
				IdCardNational:    iden.IdCardInfo.IdCardNational,
				OssIdCardNational: iden.IdCardInfo.OssIdCardNational,
				IdCardName:        iden.IdCardInfo.IdCardName,
				IdCardNumber:      iden.IdCardInfo.IdCardNumber,
				CardPeriodBegin:   iden.IdCardInfo.CardPeriodBegin,
				CardPeriodEnd:     iden.IdCardInfo.CardPeriodEnd,
			}
		}
		if iden.IdDocInfo != nil {
			r.IdentityInfoDTO.IdDocInfoDTO = &IDDocInfoV2DTO{
				IdDocCopy:      iden.IdDocInfo.IdDocCopy,
				OssIdDocCopy:   iden.IdDocInfo.OssIdDocCopy,
				IdDocName:      iden.IdDocInfo.IdDocName,
				IdDocNumber:    iden.IdDocInfo.IdDocNumber,
				DocPeriodBegin: iden.IdDocInfo.DocPeriodBegin,
				DocPeriodEnd:   iden.IdDocInfo.DocPeriodEnd,
			}
		}
	}
	if s.UboInfo != nil {
		ub := s.UboInfo
		r.UboInfoDTO = &UboInfoDTO{
			IdType:            ub.IdType,
			IdCardCopy:        ub.IdCardCopy,
			OssIdCardCopy:     ub.OssIdCardCopy,
			IdCardNational:    ub.IdCardNational,
			OssIdCardNational: ub.OssIdCardNational,
			IdDocCopy:         ub.IdDocCopy,
			OssIdDocCopy:      ub.OssIdDocCopy,
			Name:              ub.Name,
			IdNumber:          ub.IdNumber,
			IdPeriodBegin:     ub.IdPeriodBegin,
			IdPeriodEnd:       ub.IdPeriodEnd,
		}
	}
	if s.MicroBizInfo != nil {
		m := s.MicroBizInfo
		r.MicroBizInfoDTO = &MicroBizInfoDTO{
			MicroBizType:       m.MicroBizType,
			MicroStoreInfoDTO:  nil,
			MicroMobileInfoDTO: nil,
			MicroOnlineInfoDTO: nil,
		}
		if m.MicroStoreInfo != nil {
			r.MicroBizInfoDTO.MicroStoreInfoDTO = &MicroStoreInfoDTO{
				MicroName:           m.MicroStoreInfo.MicroName,
				MicroAddressCode:    m.MicroStoreInfo.MicroAddressCode,
				MicroAddress:        m.MicroStoreInfo.MicroAddress,
				StoreEntrancePic:    m.MicroStoreInfo.StoreEntrancePic,
				OssStoreEntrancePic: m.MicroStoreInfo.OssStoreEntrancePic,
				MicroIndoorCopy:     m.MicroStoreInfo.MicroIndoorCopy,
				OssMicroIndoorCopy:  m.MicroStoreInfo.OssMicroIndoorCopy,
				StoreLongitude:      m.MicroStoreInfo.StoreLongitude,
				StoreLatitude:       m.MicroStoreInfo.StoreLatitude,
			}
		}
		if m.MicroOnlineInfo != nil {
			r.MicroBizInfoDTO.MicroOnlineInfoDTO = &MicroOnlineInfoDTO{
				MicroOnlineStore: m.MicroOnlineInfo.MicroOnlineStore,
				MicroEcName:      m.MicroOnlineInfo.MicroEcName,
				MicroQrcode:      m.MicroOnlineInfo.MicroQrcode,
				OssMicroQrcode:   m.MicroOnlineInfo.OssMicroQrcode,
				MicroLink:        m.MicroOnlineInfo.MicroLink,
			}
		}
		if m.MicroMobileInfo != nil {
			r.MicroBizInfoDTO.MicroMobileInfoDTO = &MicroMobileInfoDTO{
				MicroMobileName:    m.MicroMobileInfo.MicroMobileName,
				MicroMobileCity:    m.MicroMobileInfo.MicroMobileCity,
				MicroMobileAddress: m.MicroMobileInfo.MicroMobileAddress,
				MicroMobilePics:    m.MicroMobileInfo.MicroMobilePics,
				OssMicroMobilePics: m.MicroMobileInfo.OssMicroMobilePics,
			}
		}
	}
	return r
}

func AdaptSubjectInfo(d *SubjectInfoDTO) *model.SubjectInfo {
	if d == nil {
		return nil
	}
	r := &model.SubjectInfo{
		SubjectType:              d.SubjectType,
		CertificateLetterCopy:    d.CertificateLetterCopy,
		OssCertificateLetterCopy: d.OssCertificateLetterCopy,
		IdentityInfo:             nil,
		UboInfo:                  nil,
	}
	if d.BusinessLicenseInfoDTO != nil {
		b := d.BusinessLicenseInfoDTO
		r.BusinessLicenseInfo = &model.SubBusinessLicenseInfo{
			LicenseCopy:    b.LicenseCopy,
			OssLicenseCopy: b.OssLicenseCopy,
			LicenseNumber:  b.LicenseNumber,
			MerchantName:   b.MerchantName,
			LegalPerson:    b.LegalPerson,
		}
	}
	if d.CertificateInfoDTO != nil {
		c := d.CertificateInfoDTO
		r.CertificateInfo = &model.CertificateInfo{
			CertCopy:       c.CertCopy,
			OssCertCopy:    c.OssCertCopy,
			CertType:       c.CertType,
			CertNumber:     c.CertNumber,
			MerchantName:   c.MerchantName,
			CompanyAddress: c.CompanyAddress,
			LegalPerson:    c.LegalPerson,
			PeriodBegin:    c.PeriodBegin,
			PeriodEnd:      c.PeriodEnd,
		}
	}
	if d.OrganizationInfoDTO != nil {
		o := d.OrganizationInfoDTO
		r.OrganizationInfo = &model.OrganizationInfo{
			OrganizationCopy:    o.OrganizationCopy,
			OssOrganizationCopy: o.OssOrganizationCopy,
			OrganizationCode:    o.OrganizationCode,
			OrgPeriodBegin:      o.OrgPeriodBegin,
			OrgPeriodEnd:        o.OrgPeriodEnd,
		}
	}
	if d.IdentityInfoDTO != nil {
		i := d.IdentityInfoDTO
		r.IdentityInfo = &model.IdentityInfo{
			IdDocType: i.IdDocType,
			Owner:     i.Owner,
		}
		if i.IdDocInfoDTO != nil {
			r.IdentityInfo.IdDocInfo = &model.SubIDDocInfo{
				IdDocCopy:      i.IdDocInfoDTO.IdDocCopy,
				OssIdDocCopy:   i.IdDocInfoDTO.OssIdDocCopy,
				IdDocName:      i.IdDocInfoDTO.IdDocName,
				IdDocNumber:    i.IdDocInfoDTO.IdDocNumber,
				DocPeriodBegin: i.IdDocInfoDTO.DocPeriodBegin,
				DocPeriodEnd:   i.IdDocInfoDTO.DocPeriodEnd,
			}
		}
		if i.IdCardInfoDTO != nil {
			r.IdentityInfo.IdCardInfo = &model.SubIDCardInfo{
				IdCardCopy:        i.IdCardInfoDTO.IdCardCopy,
				OssIdCardCopay:    i.IdCardInfoDTO.OssIdCardCopy,
				IdCardNational:    i.IdCardInfoDTO.IdCardNational,
				OssIdCardNational: i.IdCardInfoDTO.OssIdCardNational,
				IdCardName:        i.IdCardInfoDTO.IdCardName,
				IdCardNumber:      i.IdCardInfoDTO.IdCardNumber,
				CardPeriodBegin:   i.IdCardInfoDTO.CardPeriodBegin,
				CardPeriodEnd:     i.IdCardInfoDTO.CardPeriodEnd,
			}
		}
	}
	if d.UboInfoDTO != nil {
		u := d.UboInfoDTO
		r.UboInfo = &model.UboInfo{
			IdType:            u.IdType,
			IdCardCopy:        u.IdCardCopy,
			OssIdCardCopy:     u.OssIdCardCopy,
			IdCardNational:    u.IdCardNational,
			OssIdCardNational: u.OssIdCardNational,
			IdDocCopy:         u.IdCardCopy,
			OssIdDocCopy:      u.OssIdCardCopy,
			Name:              u.Name,
			IdNumber:          u.IdNumber,
			IdPeriodBegin:     u.IdPeriodBegin,
			IdPeriodEnd:       u.IdPeriodEnd,
		}
	}
	if d.MicroBizInfoDTO != nil {
		m := d.MicroBizInfoDTO
		r.MicroBizInfo = &model.MicroBizInfo{
			MicroBizType:    m.MicroBizType,
			MicroStoreInfo:  nil,
			MicroMobileInfo: nil,
			MicroOnlineInfo: nil,
		}
		if m.MicroStoreInfoDTO != nil {
			r.MicroBizInfo.MicroStoreInfo = &model.MicroStoreInfo{
				MicroName:           m.MicroStoreInfoDTO.MicroName,
				MicroAddressCode:    m.MicroStoreInfoDTO.MicroAddressCode,
				MicroAddress:        m.MicroStoreInfoDTO.MicroAddress,
				StoreEntrancePic:    m.MicroStoreInfoDTO.StoreEntrancePic,
				OssStoreEntrancePic: m.MicroStoreInfoDTO.OssStoreEntrancePic,
				MicroIndoorCopy:     m.MicroStoreInfoDTO.MicroIndoorCopy,
				OssMicroIndoorCopy:  m.MicroStoreInfoDTO.OssMicroIndoorCopy,
				StoreLongitude:      m.MicroStoreInfoDTO.StoreLongitude,
				StoreLatitude:       m.MicroStoreInfoDTO.StoreLatitude,
			}
		}
		if m.MicroOnlineInfoDTO != nil {
			r.MicroBizInfo.MicroOnlineInfo = &model.MicroOnlineInfo{
				MicroOnlineStore: m.MicroOnlineInfoDTO.MicroOnlineStore,
				MicroEcName:      m.MicroOnlineInfoDTO.MicroEcName,
				MicroQrcode:      m.MicroOnlineInfoDTO.MicroQrcode,
				OssMicroQrcode:   m.MicroOnlineInfoDTO.OssMicroQrcode,
				MicroLink:        m.MicroOnlineInfoDTO.MicroLink,
			}
		}
		if m.MicroMobileInfoDTO != nil {
			r.MicroBizInfo.MicroMobileInfo = &model.MicroMobileInfo{
				MicroMobileName:    m.MicroMobileInfoDTO.MicroMobileName,
				MicroMobileCity:    m.MicroMobileInfoDTO.MicroMobileCity,
				MicroMobileAddress: m.MicroMobileInfoDTO.MicroMobileAddress,
				MicroMobilePics:    m.MicroMobileInfoDTO.MicroMobilePics,
				OssMicroMobilePics: m.MicroMobileInfoDTO.OssMicroMobilePics,
			}
		}
	}
	return r
}

// 线下门店信息
type BizStoreInfoDTO struct {
	// 门店名称
	BizStoreName string
	// 门店省市编码
	BizAddressCode string
	// 门店地址
	BizStoreAddress string
	// 门店门头照片
	StoreEntrancePic    []string
	OssStoreEntrancePic []string
	// 门店店内环境照片
	IndoorPic    []string
	OssIndoorPic []string
	// 先下场所对应商家appid
	BizSubAppId string
}

// 公众号信息
type MpInfoDTO struct {
	// 服务商公众号APPID
	MpAppId string
	// 商家公众号 appid
	MpSubAppId string
	// 公众号页面截图
	MpPics    []string
	OssMpPics []string
}

// 小程序场景
type MiniProgramInfoDTO struct {
	// 服务商小程序APPID
	MiniProgramAppId string
	// 商家小程序 appid
	MiniProgramSubAppId string
	// 小程序截图
	MiniProgramPics    []string
	OssMiniProgramPics []string
}

// app 场景
type AppInfoDTO struct {
	// 服务商应用APPID
	AppAppId string
	// 商家应用 appid
	AppSubAppId string
	// 应用页面截图
	AppPics    []string
	OssAppPics []string
}

// 互联网场景
type WebInfoDTO struct {
	// 互联网网站域名
	Domain string
	// 网站授权函
	WebAuthorisation    string
	OssWebAuthorisation string
	// 应用页面截图
	WebAppId string
}

// 企业微信场景
type WeworkInfoDTO struct {
	// 商家企业微信corpid
	SubCorpId string
	// 企业微信页截图
	WeworkPics    []string
	OssWeworkPics []string
}

// 经营场景
type SalesInfoDTO struct {
	// 经营场景类型
	SalesScenesType []string
	// 线下门店场景
	BizStoreInfo *BizStoreInfoDTO
	// 公众号场景
	MpInfo *MpInfoDTO
	// 小程序场景
	MiniProgramInfo *MiniProgramInfoDTO
	// APP 场景
	AppInfo *AppInfoDTO
	// 互联网场景
	WebInfo *WebInfoDTO
	// 企业微信场景
	WeworkInfo *WeworkInfoDTO
}

type BusinessInfoDTO struct {
	// 商户简称
	MerchantShortname string
	// 客服电话
	ServicePhone string
	// salesInfo
	SalesInfoDTO *SalesInfoDTO
}

func ConvertBusinessInfoDTO(b *model.BusinessInfo) *BusinessInfoDTO {
	if b == nil {
		return nil
	}
	r := &BusinessInfoDTO{
		MerchantShortname: b.MerchantShortname,
		ServicePhone:      b.ServicePhone,
	}
	if b.SalesInfo != nil {
		s := b.SalesInfo
		r.SalesInfoDTO = &SalesInfoDTO{
			SalesScenesType: s.SalesScenesType,
		}
		if s.BizStoreInfo != nil {
			r.SalesInfoDTO.BizStoreInfo = &BizStoreInfoDTO{
				BizStoreName:        s.BizStoreInfo.BizStoreName,
				BizAddressCode:      s.BizStoreInfo.BizAddressCode,
				BizStoreAddress:     s.BizStoreInfo.BizStoreAddress,
				StoreEntrancePic:    s.BizStoreInfo.StoreEntrancePic,
				OssStoreEntrancePic: s.BizStoreInfo.OssStoreEntrancePic,
				IndoorPic:           s.BizStoreInfo.IndoorPic,
				OssIndoorPic:        s.BizStoreInfo.OssIndoorPic,
				BizSubAppId:         s.BizStoreInfo.BizSubAppId,
			}
		}
		if s.MpInfo != nil {
			r.SalesInfoDTO.MpInfo = &MpInfoDTO{
				MpAppId:    s.MpInfo.MpAppId,
				MpSubAppId: s.MpInfo.MpSubAppId,
				MpPics:     s.MpInfo.MpPics,
				OssMpPics:  s.MpInfo.OssMpPics,
			}
		}
		if s.MiniProgramInfo != nil {
			r.SalesInfoDTO.MiniProgramInfo = &MiniProgramInfoDTO{
				MiniProgramAppId:    s.MiniProgramInfo.MiniProgramAppId,
				MiniProgramSubAppId: s.MiniProgramInfo.MiniProgramSubAppId,
				MiniProgramPics:     s.MiniProgramInfo.MiniProgramPics,
				OssMiniProgramPics:  s.MiniProgramInfo.OssMiniProgramPics,
			}
		}
		if s.AppInfo != nil {
			r.SalesInfoDTO.AppInfo = &AppInfoDTO{
				AppAppId:    s.AppInfo.AppAppId,
				AppSubAppId: s.AppInfo.AppSubAppId,
				AppPics:     s.AppInfo.AppPics,
				OssAppPics:  s.AppInfo.OssAppPics,
			}
		}
		if s.WebInfo != nil {
			r.SalesInfoDTO.WebInfo = &WebInfoDTO{
				Domain:           s.WebInfo.Domain,
				WebAuthorisation: s.WebInfo.WebAuthorisation,
				WebAppId:         s.WebInfo.WebAppId,
			}
		}
		if s.WeworkInfo != nil {
			r.SalesInfoDTO.WeworkInfo = &WeworkInfoDTO{
				SubCorpId:     s.WeworkInfo.SubCorpId,
				WeworkPics:    s.WeworkInfo.WeworkPics,
				OssWeworkPics: s.WeworkInfo.OssWeworkPics,
			}
		}
	}
	return r
}

func AdaptBusinessInfo(d *BusinessInfoDTO) *model.BusinessInfo {
	if d == nil {
		return nil
	}
	r := &model.BusinessInfo{
		MerchantShortname: d.MerchantShortname,
		ServicePhone:      d.ServicePhone,
	}
	if d.SalesInfoDTO != nil {
		s := d.SalesInfoDTO
		r.SalesInfo = &model.SalesInfo{
			SalesScenesType: s.SalesScenesType,
		}
		if s.BizStoreInfo != nil {
			b := s.BizStoreInfo
			r.SalesInfo.BizStoreInfo = &model.BizStoreInfo{
				BizStoreName:        b.BizStoreName,
				BizAddressCode:      b.BizAddressCode,
				BizStoreAddress:     b.BizStoreAddress,
				StoreEntrancePic:    b.StoreEntrancePic,
				OssStoreEntrancePic: b.OssStoreEntrancePic,
				IndoorPic:           b.IndoorPic,
				OssIndoorPic:        b.OssIndoorPic,
				BizSubAppId:         b.BizSubAppId,
			}
		}
		if s.MpInfo != nil {
			m := s.MpInfo
			r.SalesInfo.MpInfo = &model.MpInfo{
				MpAppId:    m.MpAppId,
				MpSubAppId: m.MpSubAppId,
				MpPics:     m.MpPics,
				OssMpPics:  m.OssMpPics,
			}
		}
		if s.MiniProgramInfo != nil {
			m := s.MiniProgramInfo
			r.SalesInfo.MiniProgramInfo = &model.MiniProgramInfo{
				MiniProgramAppId:    m.MiniProgramAppId,
				MiniProgramSubAppId: m.MiniProgramSubAppId,
				MiniProgramPics:     m.MiniProgramPics,
				OssMiniProgramPics:  m.OssMiniProgramPics,
			}
		}
		if s.AppInfo != nil {
			r.SalesInfo.AppInfo = &model.AppInfo{
				AppAppId:    s.AppInfo.AppAppId,
				AppSubAppId: s.AppInfo.AppSubAppId,
				AppPics:     s.AppInfo.AppPics,
				OssAppPics:  s.AppInfo.OssAppPics,
			}
		}
		if s.WebInfo != nil {
			r.SalesInfo.WebInfo = &model.WebInfo{
				Domain:           s.WebInfo.Domain,
				WebAuthorisation: s.WebInfo.WebAuthorisation,
				WebAppId:         s.WebInfo.WebAppId,
			}
		}
		if s.WeworkInfo != nil {
			r.SalesInfo.WeworkInfo = &model.WeworkInfo{
				SubCorpId:     s.WeworkInfo.SubCorpId,
				WeworkPics:    s.WeworkInfo.WeworkPics,
				OssWeworkPics: s.WeworkInfo.OssWeworkPics,
			}
		}
	}
	return r
}

// 结算规则
type SettlementInfoDTO struct {
	//    入驻结算规则ID
	SettlementId string
	// 所属行业
	QualificationType string
	// 特殊资质图片
	Qualifications    []string
	OssQualifications []string
	// 优惠费率活动ID
	ActivitiesId string
	// 优惠费率活动值
	ActivitiesRate string
	// 优惠费率活动补充材料
	ActivitiesAdditions    []string
	OssActivitiesAdditions []string
}

// 补充材料
type AdditionInfoDTO struct {
	// 法人开户承诺函
	LegalPersonCommitment    string
	OssLegalPersonCommitment string
	// 法人开户意愿视频
	LegalPersonVideo    string
	OssLegalPersonVideo string
	// 补充材料
	BusinessAdditionPics    []string
	OssBusinessAdditionPics []string
	// 补充说明
	BusinessAdditionMsg string
}

type SubMchApplymentInfoDTO struct {
	// 超级管理员信息
	ContactInfoDTO *ContactInfoV2DTO
	// 主体资料信息
	SubjectInfoDTO *SubjectInfoDTO
	// 经营资料
	BusinessInfoDTO *BusinessInfoDTO
	// 结算规则
	SettlementInfoDTO *SettlementInfoDTO
	// 结算银行账户
	BankAccountInfoDTO *AccountInfoDTO
	// 补充材料
	AdditionInfoDTO *AdditionInfoDTO
}

// 本系统目前主要支持餐饮零售，则不判断行业就直接铆钉费率id
func (info *SubMchApplymentInfoDTO) SetSettlementId() {
	switch info.SubjectInfoDTO.SubjectType {
	case "SUBJECT_TYPE_MICRO":
		info.SettlementInfoDTO.SettlementId = "703"
	case "SUBJECT_TYPE_INDIVIDUAL":
		info.SettlementInfoDTO.SettlementId = "719"
	case "SUBJECT_TYPE_ENTERPRISE":
		info.SettlementInfoDTO.SettlementId = "716"
	}
}

type SubMchApplymentDTO struct {
	ID                 int64
	ApplyNo            string
	BizApplyNo         string
	BrandID            string
	MchID              string
	ApplymentType      string
	SubApplymentInfo   *SubMchApplymentInfoDTO
	WxApplymentID      string
	ApplyState         string
	ApplyStateDesc     string
	SignURL            string
	SubMchID           string
	AuditDetail        AuditDetailDTO
	LegalValidationURL string
	CreateTime         time.Time
	UpdateTime         time.Time
}

func (s *SubMchApplymentDTO) GetApplyStateDesc() string {
	switch s.ApplyState {
	case "APPLYMENT_STATE_EDITTING":
		return "编辑中"
	case "APPLYMENT_STATE_AUDITING":
		return "审核中"
	case "APPLYMENT_STATE_REJECTED":
		return "已驳回"
	case "APPLYMENT_STATE_TO_BE_CONFIRMED":
		return "待账户验证"
	case "APPLYMENT_STATE_TO_BE_SIGNED":
		return "待签约"
	case "APPLYMENT_STATE_SIGNING":
		return "开通权限中"
	case "APPLYMENT_STATE_FINISHED":
		return "已完成"
	case "APPLYMENT_STATE_CANCELED":
		return "已作废"
	default:
		return ""
	}
}

func ConvertSubApplymentInfo(info *model.SubApplymentInfo) *SubMchApplymentInfoDTO {
	r := &SubMchApplymentInfoDTO{}
	if info.ContactInfo != nil {
		r.ContactInfoDTO = &ContactInfoV2DTO{
			ContactName:     info.ContactInfo.ContactName,
			ContactIdNumber: info.ContactInfo.ContactIdNumber,
			MobilePhone:     info.ContactInfo.MobilePhone,
			ContactEmail:    info.ContactInfo.ContactEmail,
		}
	}
	r.SubjectInfoDTO = ConvertSubjectInfo(info.SubjectInfo)
	r.BusinessInfoDTO = ConvertBusinessInfoDTO(info.BusinessInfo)
	if info.SettlementInfo != nil {
		set := info.SettlementInfo
		r.SettlementInfoDTO = &SettlementInfoDTO{
			SettlementId:           set.SettlementId,
			QualificationType:      set.QualificationType,
			Qualifications:         set.Qualifications,
			OssQualifications:      set.OssQualifications,
			ActivitiesId:           set.ActivitiesId,
			ActivitiesRate:         set.ActivitiesRate,
			ActivitiesAdditions:    set.ActivitiesAdditions,
			OssActivitiesAdditions: set.OssActivitiesAdditions,
		}
	}
	if info.BankAccountInfo != nil {
		ba := info.BankAccountInfo
		r.BankAccountInfoDTO = &AccountInfoDTO{
			BankAccountType: ba.BankAccountType,
			AccountBank:     ba.AccountBank,
			AccountName:     ba.AccountName,
			BankAddressCode: ba.BankAddressCode,
			BankBranchID:    ba.BankBranchID,
			BankName:        ba.BankName,
			AccountNumber:   ba.AccountNumber,
		}
	}
	if info.AdditionInfo != nil {
		ad := info.AdditionInfo
		r.AdditionInfoDTO = &AdditionInfoDTO{
			LegalPersonCommitment:   ad.LegalPersonCommitment,
			LegalPersonVideo:        ad.LegalPersonVideo,
			OssLegalPersonVideo:     ad.OssLegalPersonVideo,
			BusinessAdditionPics:    ad.BusinessAdditionPics,
			OssBusinessAdditionPics: ad.OssBusinessAdditionPics,
			BusinessAdditionMsg:     ad.BusinessAdditionMsg,
		}
	}
	return r
}

func AdaptSubApplymentInfo(d *SubMchApplymentInfoDTO) *model.SubApplymentInfo {
	if d == nil {
		return nil
	}
	r := &model.SubApplymentInfo{}
	if d.ContactInfoDTO != nil {
		c := d.ContactInfoDTO
		r.ContactInfo = &model.SubContactInfo{
			ContactName:     c.ContactName,
			ContactIdNumber: c.ContactIdNumber,
			MobilePhone:     c.MobilePhone,
			ContactEmail:    c.ContactEmail,
		}
	}
	r.SubjectInfo = AdaptSubjectInfo(d.SubjectInfoDTO)
	r.BusinessInfo = AdaptBusinessInfo(d.BusinessInfoDTO)
	if d.SettlementInfoDTO != nil {
		s := d.SettlementInfoDTO
		r.SettlementInfo = &model.SettlementInfo{
			SettlementId:           s.SettlementId,
			QualificationType:      s.QualificationType,
			Qualifications:         s.Qualifications,
			OssQualifications:      s.OssQualifications,
			ActivitiesId:           s.ActivitiesId,
			ActivitiesRate:         s.ActivitiesRate,
			ActivitiesAdditions:    s.ActivitiesAdditions,
			OssActivitiesAdditions: s.OssActivitiesAdditions,
		}
	}
	if d.BankAccountInfoDTO != nil {
		b := d.BankAccountInfoDTO
		r.BankAccountInfo = &model.AccountInfo{
			BankAccountType: b.BankAccountType,
			AccountBank:     b.AccountBank,
			AccountName:     b.AccountName,
			BankAddressCode: b.BankAddressCode,
			BankBranchID:    b.BankBranchID,
			BankName:        b.BankName,
			AccountNumber:   b.AccountNumber,
		}
	}
	if d.AdditionInfoDTO != nil {
		a := d.AdditionInfoDTO
		r.AdditionInfo = &model.AdditionInfo{
			LegalPersonCommitment:   a.LegalPersonCommitment,
			LegalPersonVideo:        a.LegalPersonVideo,
			OssLegalPersonVideo:     a.OssLegalPersonVideo,
			BusinessAdditionPics:    a.BusinessAdditionPics,
			OssBusinessAdditionPics: a.OssBusinessAdditionPics,
			BusinessAdditionMsg:     a.BusinessAdditionMsg,
		}
	}
	// 设置进件费率id
	r.SetSettlementId()
	return r
}

func ConvertSubMchApplyment(apply *model.Applyment) *SubMchApplymentDTO {
	if apply == nil {
		return nil
	}
	r := &SubMchApplymentDTO{
		ID:                 apply.ID,
		ApplyNo:            apply.ApplyNo,
		BizApplyNo:         apply.BizApplyNo,
		BrandID:            apply.BrandID,
		MchID:              apply.MchID,
		ApplymentType:      apply.ApplymentType,
		WxApplymentID:      util.ConvertNullInt64(apply.WxApplymentID),
		ApplyState:         util.ConvertNullString(apply.ApplyState),
		ApplyStateDesc:     util.ConvertNullString(apply.ApplyStateDesc),
		SignURL:            util.ConvertNullString(apply.SignURL),
		SubMchID:           util.ConvertNullString(apply.SubMchID),
		LegalValidationURL: util.ConvertNullString(apply.LegalValidationURL),
		CreateTime:         apply.CreateTime,
		UpdateTime:         apply.UpdateTime,
	}
	if apply.ApplymentInfo != nil {
		info := &model.SubApplymentInfo{}
		info.ToStruct(apply.ApplymentInfo)
		r.SubApplymentInfo = ConvertSubApplymentInfo(info)
	}
	if apply.AuditDetail != nil {
		aud := model.AuditDetails{}
		aud.ToStruct(apply.AuditDetail)
		r.AuditDetail = convertAuditDetail(aud)
	}
	return r
}

// 特约商户进件
func AdaptSubApplyment(d *SubMchApplymentDTO) *model.Applyment {
	if d == nil {
		return nil
	}
	r := &model.Applyment{
		ID:                 d.ID,
		ApplyNo:            d.ApplyNo,
		BizApplyNo:         d.BizApplyNo,
		BrandID:            d.BrandID,
		MchID:              d.MchID,
		ApplymentType:      d.ApplymentType,
		WxApplymentID:      util.AdaptNullInt64(d.WxApplymentID),
		ApplyState:         util.AdaptNullString(d.ApplyState),
		ApplyStateDesc:     util.AdaptNullString(d.ApplyStateDesc),
		SignURL:            util.AdaptNullString(d.SignURL),
		AccountValidation:  nil,
		SubMchID:           util.AdaptNullString(d.SubMchID),
		AuditDetail:        nil,
		LegalValidationURL: util.AdaptNullString(d.LegalValidationURL),
		CreateTime:         d.CreateTime,
		UpdateTime:         d.UpdateTime,
	}
	if d.SubApplymentInfo != nil {
		info := AdaptSubApplymentInfo(d.SubApplymentInfo)
		b, _ := json.Marshal(info)
		r.ApplymentInfo = b
	}
	if d.AuditDetail != nil {
		au := AdaptAuditDetail(d.AuditDetail)
		b, _ := json.Marshal(au)
		r.AuditDetail = b
	}
	return r
}
